#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess.
#
# This version is for packages that are architecture dependent.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

_PWD=`pwd`

include omd.make
-include .config

.PHONY: clean binary-indep binary-arch binary install

clean:
	dh_testdir
	dh_testroot

	$(MAKE) EDITION=$(EDITION) clean
	rm -f $(_PWD)/debian/postinst
	rm -f $(_PWD)/debian/preinst
	rm -f $(_PWD)/debian/prerm
	rm -f $(_PWD)/debian/postrm
	rm -f $(_PWD)/debian/links
	rm -f $(_PWD)/debian/*.debhelper
	rm -rf $(_PWD)/debian/check-mk-*-[0-9].[0-9.]*
	rm -rf $(_PWD)/debian/check-mk-*-[1-9][0-9].[0-9.]*

	dh_clean

install:
	dh_testdir
	#dh_testroot
	# Keep the package installation directory for incremental builds. In case a
	# fresh build is requested, debuild will be called with "clean"
	dh_prep --exclude=`dh_listpackages`
	dh_installdirs

	# This target compiles and installs everything needed into our DESTDIR
	$(MAKE) EDITION=$(EDITION) DESTDIR=$(_PWD)/debian/`dh_listpackages` install

	# Remove version independent things that will be created later by postinst
	set -e ; \
	for F in usr/bin/omd \
		 usr/bin \
		 usr/share/man/man8/omd.8.gz \
		 usr/share/man/man8 \
		 usr/share/man \
		 usr/share \
		 usr \
		 omd \
		 opt/omd/apache \
		 opt/omd/sites \
		 opt/omd/versions/default \
		 etc/apache2/conf.d/zzz_omd.conf \
		 etc/apache2/conf.d \
		 etc/apache2 \
		 etc/init.d/omd \
		 etc/systemd/system/omd.service; do \
	    ABS_PATH="$(_PWD)/debian/`dh_listpackages`/$$F" ; \
            if [ -f "$$ABS_PATH" ] || [ -h "$$ABS_PATH" ] ; then \
                rm -f "$$ABS_PATH" ; \
            elif [ -d "$$ABS_PATH" ]; then \
                rmdir "$$ABS_PATH" ; \
            fi \
	done

	sed -e 's/{CMK_VERSION}.{CMK_EDITION}/$(OMD_VERSION)/g' \
	   $(_PWD)/debian/postinst.in > $(_PWD)/debian/postinst
	sed -e 's/{CMK_VERSION}.{CMK_EDITION}/$(OMD_VERSION)/g' \
	   $(_PWD)/debian/preinst.in > $(_PWD)/debian/preinst
	sed -e 's/{CMK_VERSION}.{CMK_EDITION}/$(OMD_VERSION)/g' \
	   $(_PWD)/debian/prerm.in > $(_PWD)/debian/prerm
	sed -e 's/{CMK_VERSION}.{CMK_EDITION}/$(OMD_VERSION)/g' \
	   $(_PWD)/debian/postrm.in > $(_PWD)/debian/postrm
	sed -e 's/{CMK_VERSION}.{CMK_EDITION}/$(OMD_VERSION)/g' \
	   $(_PWD)/debian/links.in > $(_PWD)/debian/links

binary-indep:

# Please note: This target is run in context of "fakeroot" (see "man
# dpkg-buildpackage") for further information.
# In the past we used this phase for building our packages, but we experienced
# some hardly reproducable random IO errors when executing our build in this
# phase. For this reason we now build our stuff in the build-arch phase which
# is executed in the current users context.
binary-arch:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
#	dh_install
#	dh_installmenu
	dh_installdebconf -n
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installcatalogs
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_installwm
#	dh_installudev
#	dh_lintian
#	dh_bugfiles
#	dh_undocumented
	dh_installman
	dh_link
# Is done by "omd/strip_binaries" script for all package types just like dh_strip would do
#	dh_strip --exclude=wkhtmltopdf --exclude=cmc --exclude=waitmax --exclude agent_modbus
	dh_compress --exclude=README.md
	dh_fixperms
#	dh_perl
#	dh_makeshlibs
	dh_installdeb
#	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

build-arch: install

binary: binary-indep binary-arch
